<?xml version="1.0" encoding="gb2312"?>
<!-- The formatting in this file is "wacky" so that the SQL it generates won't be -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html" indent="no" encoding="gb2312" />
  <xsl:variable name="rootNode">resultset</xsl:variable>
  <xsl:variable name="recordNode">result</xsl:variable>
  <xsl:variable name="quote">'</xsl:variable>
  <xsl:variable name="code">0</xsl:variable>
  <!-- Handle Singleton Saved Queries -->
  <xsl:template match="savedquery">
    <xsl:call-template name="buildSQL" />
  </xsl:template>
  <!-- Handle Bulk Saved Queries -->
  <xsl:template match="resultset">
    <xsl:for-each select="result">
      <xsl:if test="$code != returnedtypecode">
        <xsl:call-template name="writeHeader">
          <xsl:with-param name="title">
            <xsl:choose>
              <xsl:when test="returnedtypecode = 1">ACCOUNTS</xsl:when>
              <xsl:when test="returnedtypecode = 2">CONTACTS</xsl:when>
              <xsl:when test="returnedtypecode = 3">OPPORTUNITIES</xsl:when>
              <xsl:when test="returnedtypecode = 4">LEADS</xsl:when>
              <xsl:when test="returnedtypecode = 5">NOTES</xsl:when>
              <xsl:when test="returnedtypecode = 112">CASES</xsl:when>
              <xsl:when test="returnedtypecode = 123">COMPETITORS</xsl:when>
              <xsl:when test="returnedtypecode = 126">DOCUMENT INDEX</xsl:when>
              <xsl:when test="returnedtypecode = 127">KB ARTICLES</xsl:when>
              <xsl:when test="returnedtypecode = 132">NEWS ARTICLES</xsl:when>
              <xsl:when test="returnedtypecode = 134">ACTIVITIES</xsl:when>
              <xsl:when test="returnedtypecode = 10">BUSINESSES</xsl:when>
              <xsl:when test="returnedtypecode = 8">SYSTEM USERS</xsl:when>
              <xsl:when test="returnedtypecode = 9">TEAMS</xsl:when>
              <xsl:when test="returnedtypecode = 1001">ACTIVITY MIME ATTACHMENTS</xsl:when>
              <xsl:when test="returnedtypecode = 1010">CONTRACTS</xsl:when>
              <xsl:when test="returnedtypecode = 1011">CONTRACT DETAILS</xsl:when>
              <xsl:when test="returnedtypecode = 1016">KB ARTICLE TEMPLATES</xsl:when>
              <xsl:when test="returnedtypecode = 1022">PRICE LEVELS</xsl:when>
              <xsl:when test="returnedtypecode = 1024">PRODUCTS</xsl:when>
              <xsl:when test="returnedtypecode = 1026">PRODUCT PRICE LEVELS</xsl:when>
              <xsl:when test="returnedtypecode = 1036">ROLES</xsl:when>
              <xsl:when test="returnedtypecode = 1038">SALES LITERATURES</xsl:when>
              <xsl:when test="returnedtypecode = 1039">SAVED QUERIES (VIEWS)</xsl:when>
              <xsl:when test="returnedtypecode = 1013">DISCOUNTS</xsl:when>
              <xsl:when test="returnedtypecode = 1071">CUSTOMER ADDRESS</xsl:when>
              <xsl:when test="returnedtypecode = 1080">DISCOUNT TYPES</xsl:when>
              <xsl:when test="returnedtypecode = 1082">KB ARTICLE COMMENTS</xsl:when>
              <xsl:when test="returnedtypecode = 1083">OPPORTUNITY PRODUCTS</xsl:when>
              <xsl:when test="returnedtypecode = 1089">ORDER PRODUCTS</xsl:when>
              <xsl:when test="returnedtypecode = 1055">UNIT OF MEASURES</xsl:when>
              <xsl:when test="returnedtypecode = 1056">UNIT OF MEASURE SCHEDULES</xsl:when>
              <xsl:when test="returnedtypecode = 1070">SALES LITERATURE DOCUMENTS (ITEMS)</xsl:when>
              <xsl:when test="returnedtypecode = 1084">QUOTES</xsl:when>
              <xsl:when test="returnedtypecode = 1085">QUOTE PRODUCTS</xsl:when>
              <xsl:when test="returnedtypecode = 1088">ORDERS</xsl:when>
              <xsl:when test="returnedtypecode = 1090">INVOICES</xsl:when>
              <xsl:when test="returnedtypecode = 1091">INVOICE PRODUCTS</xsl:when>
              <xsl:when test="returnedtypecode = 2010">EMAIL TEMPLATE</xsl:when>
              <xsl:when test="returnedtypecode = 2011">CONTRACT TEMPLATE</xsl:when>
              <xsl:otherwise>UNKNOWN</xsl:otherwise>
            </xsl:choose>
            <xsl:text> (</xsl:text>
            <xsl:value-of select="returnedtypecode" />
            <xsl:text>)</xsl:text>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:if>
      <xsl:variable name="code" select="returnedtypecode" />
      <xsl:call-template name="buildSQL" />
---------------------------------------------------------------------------------------------
	</xsl:for-each>
  </xsl:template>
  <!-- Generate the SQL for "Extra Data.sql" -->
  <xsl:template name="buildSQL">
INSERT INTO SavedQueryBase
(
SavedQueryId,
OrganizationId,
Name,
Description,
IsDefault,
IsCustomizable,
IsQuickFindQuery,
QueryType,
ReturnedTypeCode,
QueryAPI,
FetchXml,
LayoutXml,
GridXsl,
ExportXsl,
PrintXsl,
ColumnSetXml,
SecurityDescriptor
)
VALUES
(
'<xsl:value-of select="savedqueryid" />',
'{00000000-0000-0000-00aa-000000000000}',
'<xsl:value-of select="name" />',
'<xsl:call-template name="fixSQL"><xsl:with-param name="text" select="description" /></xsl:call-template>',
<xsl:value-of select="isdefault" />,
<xsl:value-of select="iscustomizable" />,
<xsl:value-of select="isquickfindquery" />,
<xsl:value-of select="querytype" />,
<xsl:value-of select="returnedtypecode" />,
<xsl:choose><xsl:when test="queryapi">'<xsl:value-of select="queryapi" />',</xsl:when><xsl:otherwise>NULL,
</xsl:otherwise></xsl:choose><xsl:choose><xsl:when test="fetchxml and not(queryapi)">'<xsl:call-template name="fixSQL"><xsl:with-param name="text" select="normalize-space(fetchxml)" /></xsl:call-template>',</xsl:when><xsl:otherwise>
NULL,</xsl:otherwise></xsl:choose>
'<xsl:call-template name="fixSQL"><xsl:with-param name="text" select="normalize-space(layoutxml)" /></xsl:call-template>', 
'<xsl:call-template name="fixSQL"><xsl:with-param name="text" select="normalize-space(gridxsl)" /></xsl:call-template>',
'<xsl:call-template name="fixSQL"><xsl:with-param name="text" select="normalize-space(exportxsl)" /></xsl:call-template>',
'<xsl:call-template name="fixSQL"><xsl:with-param name="text" select="normalize-space(printxsl)" /></xsl:call-template>',
<xsl:choose><xsl:when test="columnsetxml and queryapi">'<xsl:call-template name="fixSQL"><xsl:with-param name="text" select="normalize-space(columnsetxml)" /></xsl:call-template>',</xsl:when><xsl:otherwise>NULL,</xsl:otherwise></xsl:choose>
'O:SYG:BA'
)
</xsl:template>
  <!-- Converts ' into '' so that SQL Server will not choke -->
  <xsl:template name="fixSQL">
    <xsl:param name="text" />
    <xsl:choose>
      <xsl:when test="contains($text,$quote)">
        <xsl:value-of select="substring-before($text,$quote)" />''<xsl:call-template name="fixSQL"><xsl:with-param name="text"><xsl:value-of select="substring-after($text,$quote)" /></xsl:with-param></xsl:call-template></xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <!-- Generates an "Extra Data.SQL" Header section with a spacing on the top -->
  <xsl:template name="writeHeader">
    <xsl:param name="title" />




-- ==========================================================================================
--  <xsl:value-of select="$title" />
-- ==========================================================================================
</xsl:template>
</xsl:stylesheet>